package com.ding.second.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ding.second.service.ProductServer;

@Controller
@RequestMapping("/inquiry")
public class InquiryAction {

	@Autowired
	private ProductServer productServer;

	@RequestMapping("/all")
	public String getAllCommdity(Model model) {
		Map<String, Object> productInfo = new HashMap<String, Object>();
		productInfo.put("productState", "101");
		List<Map<String, Object>> productList = productServer.getProjects(productInfo);
		for (int i = 0; i < productList.size(); i++) {
			String currentName = (String) productList.get(i).get("imageName");
			productList.get(i).put("imageName", "productImages/" + currentName);
			
			String currentAvata = (String) productList.get(i).get("userAvatar");
			productList.get(i).put("userAvatar", "avatar/" + currentAvata);
		}

		model.addAttribute("productList", productList);

		return "/commodityInquiry";
	}

	@RequestMapping("/search")
	public String getCommdity(Model model, String searchInput) {
		Map<String, Object> productInfo = new HashMap<String, Object>();
		productInfo.put("productState", "101");
		if (searchInput.contains("@")) {
			String userNames = searchInput.substring(1).toString();
			productInfo.put("userName", userNames);
		} else {
			productInfo.put("productName", searchInput);
		}
		List<Map<String, Object>> productList = productServer.getProjects(productInfo);
		
		for (int i = 0; i < productList.size(); i++) {
			String currentName = (String) productList.get(i).get("imageName");
			productList.get(i).put("imageName", "productImages/" + currentName);
			
			String currentAvata = (String) productList.get(i).get("userAvatar");
			productList.get(i).put("userAvatar", "avatar/" + currentAvata);
		}

		model.addAttribute("searchInput", searchInput);
		model.addAttribute("productList", productList);
		return "/commodityInquiry";
	}

	@RequestMapping("/user/{userName}")
	public String getCommdityByUser(Model model, @PathVariable String userName) {
		Map<String, Object> productInfo = new HashMap<String, Object>();
		productInfo.put("userName", userName);
		productInfo.put("productState", "101");

		List<Map<String, Object>> productList = productServer.getProjects(productInfo);
		for (int i = 0; i < productList.size(); i++) {
			String currentName = (String) productList.get(i).get("imageName");
			productList.get(i).put("imageName", "productImages/" + currentName);
			
			String currentAvata = (String) productList.get(i).get("userAvatar");
			productList.get(i).put("userAvatar", "avatar/" + currentAvata);
		}
		
		model.addAttribute("productList", productList);
		model.addAttribute("searchInput", "@" + userName);
		return "/commodityInquiry";
	}
	
	@RequestMapping("/type/{typeId}")
	public String getCommdityByType(Model model, @PathVariable String typeId) {
		Map<String, Object> productInfo = new HashMap<String, Object>();
		productInfo.put("typeId", typeId);
		productInfo.put("productState", "101");

		List<Map<String, Object>> productList = productServer.getProjects(productInfo);
		for (int i = 0; i < productList.size(); i++) {
			String currentName = (String) productList.get(i).get("imageName");
			productList.get(i).put("imageName", "productImages/" + currentName);
			
			String currentAvata = (String) productList.get(i).get("userAvatar");
			productList.get(i).put("userAvatar", "avatar/" + currentAvata);
		}
		
		model.addAttribute("productList", productList);
		return "/commodityInquiry";
	}

}
